// 定义跟社区食谱相关的路由
const Router = require('koa-router');
// 创建一个新的 Router实例
const router = new Router();
const { verify } = require('../utils/jwt.js');

const { publishCook, getCommonCook, getCommonById, getMyPublish, getMyDraft, changePublish, updateCook, getCookByHome, getCookByName, deleteMyCook, getRecommendDaily } = require('../controllers/index.js');

router.prefix('/common')

router.post('/publishCook', verify(), async (ctx, next) => {
  const result = ctx.request.body
  try {
    const res = await publishCook(result)
    if (res) {
      ctx.body = {
        code: 200,
        message: '发布成功'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器异常'
    }
  }
})

router.get('/getCommonCook', async (ctx, next) => {
  try {
    const res = await getCommonCook();
    if (res.length) {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.get('/getCommonById', async (ctx, next) => {
  const id = ctx.query.id
  try {
    const res = await getCommonById(id);
    if (res.length) {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.get('/getMyPublish', async (ctx, next) => {
  const userId = ctx.query.userId
  try {
    const res = await getMyPublish(userId);
    if (res.length) {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.get('/getMyDraft', async (ctx, next) => {
  const userId = ctx.query.userId
  try {
    const res = await getMyDraft(userId);
    if (res.length) {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.get('/changePublish', async (ctx, next) => {
  const { id, publishTime } = ctx.query
  try {
    const res = await changePublish(id, publishTime);
    if (res) {
      ctx.body = {
        code: 200,
        message: '发布成功'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.post('/updateCook', async (ctx, next) => {
  const { id, param } = ctx.request.body;

  try {
    const res = await updateCook(id, param)
    if (res) {
      ctx.body = {
        code: 200,
        message: '发布成功'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器异常'
    }
  }
})

router.get('/getCookByHome', async (ctx, next) => {
  try {
    const res = await getCookByHome();

    if (res.length) {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.get('/getCookByName', async (ctx, next) => {
  const { cookName } = ctx.query
  try {
    const res = await getCookByName(cookName)

    if (res.length) {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.get('/deleteMyCook', async (ctx, next) => {
  const { id } = ctx.query
  try {
    const res = await deleteMyCook(id)
    ctx.body = {
      code: 200,
      data: res,
      message: '删除成功'
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.get('/getRecommendDaily', async (ctx, next) => {
  const { start, end } = ctx.query
  try {
    const res = await getRecommendDaily(start, end)
    if (res.length > 0) {
      ctx.body = {
        code: 200,
        data: res[0],
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: null,
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

module.exports = {
  commonRouter: router
};